#include "menuPlaylist.h"
#include "errorscreen.h"

int abortPaint = 0;

SDL_Surface * background;
SDL_Surface * file_icon;
SDL_Surface * play_icon;
TTF_Font * entry_font;



int getPlaylistContent(PlaylistMenuState *state, mpd_Connection *con)
{

    int count = 0;
    int reloadPlaylist = 0;
    //printf("Trayendome la playlist\n");

    mpd_sendStatusCommand(con);
    mpd_Status * status = mpd_getStatus(con);
    if (status)
    {
        //printf("Pongo state!");
        state->state = status->state;
        //printf("Pongo random");
        state->random = status->random;

        if (status->state == MPD_STATUS_STATE_PLAY || status->state
                == MPD_STATUS_STATE_PLAY)
        {
            //printf("Current song");
            state->currentSong = status->song;

        }

        if (status->playlist != state->playlistVersion)
        {
            //printf("Hay que recargar");
            reloadPlaylist = 1;
        }
        //printf("Free Status!");
        mpd_freeStatus(status);

    }
    else
    {
        app_error("Status es NULL!!!");
    }

    mpd_finishCommand(con);

    if (reloadPlaylist)
    {
        //printf("Reload Playlist!");
        mpd_sendPlaylistInfoCommand(con, -1);

        if (con->error)
        {
            char msg[50];
            sprintf(msg,"Error: %s, %d", con->errorStr, con->errorCode);
            app_error(msg);
        }

        mpd_InfoEntity * entity;
        while ((entity = mpd_getNextInfoEntity(con)) && count
                < MAX_PLAYLIST_LENGTH)
        {

            if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
            {
                //printf("Count: %d\n",count);
                PlaylistEntry * entry = &state->entries[count];
                mpd_Song * song = entity->info.song;

                //printf("Descripcion!");
                if (song->artist != NULL && song->title != NULL)
                {
                    strcpy(entry->description, song->artist);
                    strcat(entry->description, " - ");
                    strcat(entry->description, song->title);
                }
                else
                {

                    getDescriptionFromPath(song->file,entry->description);
                }

                entry->id = song->id;
                entry->pos = song->pos;

                mpd_freeInfoEntity(entity);

                count++;
            }
        }

        state->lines = count;
        state->selectedLine = 0;
    }

    return 1;

}

menu * createPlaylistMenu()
{
    menu *playlistMenu = (menu *) malloc(sizeof(menu));

    //Set up handlers
    playlistMenu->handleC = handleCPlaylistMenu;
    playlistMenu->handleDown = handleDownPlaylistMenu;
    playlistMenu->handleLT = handleLTPlaylistMenu;
    playlistMenu->handleRT = handleRTPlaylistMenu;
    playlistMenu->handleS = handleSPlaylistMenu;
    playlistMenu->handleT = handleTPlaylistMenu;
    playlistMenu->handleUp = handleUpPlaylistMenu;
    playlistMenu->handleX = handleXPlaylistMenu;

    //Set up config calls
    playlistMenu->aboutToHide = aboutToHidePlaylistMenu;
    playlistMenu->aboutToShow = aboutToShowPlaylistMenu;
    playlistMenu->init = initPlaylistMenu;
    playlistMenu->destroy = destroyPlaylistMenu;

    //State
    PlaylistMenuState * state = malloc(sizeof(PlaylistMenuState));

    state->currentSong = 0;
    state->selectedLine = 0;
    state->lines = 0;
    state->random = 0;
    state->state = 0;
    state->playlistVersion = -1;

    playlistMenu->state = state;

    entry_font = sdl_load_font("FreeMonoBold.ttf",12);
    background = sdl_load_image("fondofiles.png");
    file_icon = sdl_load_image("music.png");
    play_icon = sdl_load_image("play.png");

    return playlistMenu;
}

int handleXPlaylistMenu(void *vstate, mpd_Connection* connection)
{
    PlaylistMenuState *state = (PlaylistMenuState *) vstate;
    if (state->lines > 0)
    {
        mpd_sendPlayIdCommand(connection,
                              state->entries[state->selectedLine].id);
        state->currentSong = state->selectedLine;
        mpd_finishCommand(connection);
        paintPlaylistMenu(state, connection);
    }
    return 1;
}

int handleTPlaylistMenu(void *vstate, mpd_Connection* connection)
{

    PlaylistMenuState * state = (PlaylistMenuState *) vstate;
    getPlaylistContent(state, connection);
    paintPlaylistMenu(state, connection);
    return 1;
}

int handleSPlaylistMenu(void *vstate, mpd_Connection* connection)
{
    return 1;
}

int handleCPlaylistMenu(void *vstate, mpd_Connection* connection)
{
    return 1;
}

int handleUpPlaylistMenu(void *vstate, mpd_Connection* connection)
{

    PlaylistMenuState * menuState = (PlaylistMenuState *) vstate;
    menuState->selectedLine--;
    if (menuState->selectedLine < 0)
        menuState->selectedLine = 0;
    else
        paintPlaylistMenu(menuState, connection);

    return 1;
}

int handleDownPlaylistMenu(void *vstate, mpd_Connection* connection)
{
    PlaylistMenuState * menuState = (PlaylistMenuState *) vstate;
    if (menuState->selectedLine != (menuState->lines - 1))
    {
        menuState->selectedLine++;
        paintPlaylistMenu(menuState, connection);
    }

    return 1;
}

int handleRTPlaylistMenu(void *vstate, mpd_Connection* connection)
{
    PlaylistMenuState *state = (PlaylistMenuState *) (vstate);

    mpd_sendNextCommand(connection);
    mpd_finishCommand(connection);

    getPlaylistContent(state, connection);
    paintPlaylistMenu(vstate, connection);

    return 1;
}

int handleLTPlaylistMenu(void *vstate, mpd_Connection* connection)
{
    PlaylistMenuState *state = (PlaylistMenuState *) (vstate);

    mpd_sendPrevCommand(connection);
    mpd_finishCommand(connection);

    getPlaylistContent(state, connection);
    paintPlaylistMenu(vstate, connection);
    return 1;
}

int initPlaylistMenu(void *vstate, mpd_Connection* connection)
{
    //PlaylistMenuState *state = (PlaylistMenuState *) (vstate);


    return 1;

}

int paintPlaylistMenu(void *vstate, mpd_Connection* connection)
{
    int inicio = 0;
    int cuantos = 0;
    int idx = 0;

    PlaylistMenuState * menuState = (PlaylistMenuState *) vstate;
    if (menuState->lines > 0)


    inicio = menuState->selectedLine - (MAX_LINES - 1) ;
    if (inicio < 0)
        inicio = 0;

    cuantos = inicio + ((menuState->lines < MAX_LINES) ? menuState->lines : MAX_LINES);

    sdl_clear_screen(0,0,0);
    SDL_Rect position = {0,0};
    sdl_paint_surface(background,&position);

    SDL_Surface * txt = sdl_draw_text(entry_font,255,255,255,0,0,0,0,0,"Playlist browser",blended);

    position.x = 5;
    position.y = 0;
    sdl_paint_surface(txt,&position);

    for (; inicio < cuantos; inicio++, idx++)
    {

        PlaylistEntry entry = menuState->entries[inicio];
        SDL_Surface * text;
        SDL_Surface * icon = file_icon;

        if(inicio == menuState->selectedLine)
        {
            text = sdl_draw_text(entry_font,255,100,100,0,0,0,0,0,entry.description,blended);
        }
        else
        {
            text = sdl_draw_text(entry_font,255,255,255,0,0,0,0,0,entry.description,blended);
        }

        if(inicio == menuState->currentSong)
        {
            icon = play_icon;
        }

        SDL_Rect iconRect;
        iconRect.y = 15 * (idx+1);
        iconRect.x = 5;

        sdl_paint_surface(icon,&iconRect);

        SDL_Rect textRect;

        textRect.x = 26;
        textRect.y = 15 * (idx+1);
        textRect.w = 272-26;
        textRect.h = text->h;

        sdl_paint_surface(text,&textRect);
        SDL_FreeSurface(text);



    }

    sdl_flip();



    return 1;
}

int aboutToShowPlaylistMenu(void *vstate, mpd_Connection* connection)
{

    PlaylistMenuState * state = (PlaylistMenuState *) vstate;

    getPlaylistContent(state, connection);
    paintPlaylistMenu(vstate, connection);

    //SceSize sizeState = sizeof(PlaylistMenuState);

    //sceKernelStartThread(state->threadUID, sizeof(PlaylistMenuState *), state);

    return 1;
}

int aboutToHidePlaylistMenu(void *vstate, mpd_Connection* connection)
{
    PlaylistMenuState * state = (PlaylistMenuState *) state;
    //sceKernelTerminateThread(state->threadUID);
    return 1;
}

int destroyPlaylistMenu(void* vstate, mpd_Connection* connection)
{

    return 1;
}
